package com.iyjhsoc.game.controller;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iyjhsoc.game.domain.VO.BaseVO;
import com.iyjhsoc.game.domain.response.Response;
import com.iyjhsoc.game.exception.BusinessException;
import com.iyjhsoc.game.util.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;


public  abstract class BaseController<DAO extends BaseMapper<DO>,DO,DTO extends BaseVO> {

    @Autowired
    DAO dao;

    @PostMapping("queryList")
    public Response queryList(@RequestBody DTO dto){
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<DO> ms = dao.selectList(QueryUtil.createQueryWrapper(dto));
        return Response.SUCCESS(new PageInfo(ms));
    }


    @GetMapping("deleteById")
    public boolean deleteById(String id) {
        int i = dao.deleteById(id);
        if(i >1){
            throw new BusinessException("主键重复请检查数据库!!!!!");
        }
        return i == 1;
    }

    @PostMapping("updateById")
    public Response updateById(@RequestBody DO entity){
        int i = dao.updateById(entity);
        if(i >1){
            throw new BusinessException("主键重复请检查数据库!!!!!");
        }
        return Response.SUCCESS("成功");
    }

    @PostMapping("save")
    public Response save(@RequestBody DO entity){
        int insert = dao.insert(entity);
        if(insert == 1){
            return Response.SUCCESS("成功");
        }
       return Response.ERROR("失败");
    }

    @GetMapping("getById")
    public Response getById(String id){
        DO aDo = dao.selectById(id);
        return Response.SUCCESS(aDo);
    }


}
